package com.example.spider.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;

public class HeaderUtil {

    private static final List<String> USER_AGENT_LIST = new ArrayList<>();

    static {
        USER_AGENT_LIST.add("Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");
        USER_AGENT_LIST.add("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.1 Safari/537.36");
        USER_AGENT_LIST.add("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36");
        USER_AGENT_LIST.add("Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2226.0 Safari/537.36");
        USER_AGENT_LIST.add("Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; AS; rv:11.0) like Gecko");
        //USER_AGENT_LIST.add("Mozilla/5.0 (compatible, MSIE 11, Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko"); // 部分网站403
        USER_AGENT_LIST.add("Mozilla/5.0 (compatible; MSIE 10.6; Windows NT 6.1; Trident/5.0; InfoPath.2; SLCC1; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 2.0.50727) 3gpp-gba UNTRUSTED/1.0");
        USER_AGENT_LIST.add("Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 7.0; InfoPath.3; .NET CLR 3.1.40767; Trident/6.0; en-IN)");
        USER_AGENT_LIST.add("Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)");
        USER_AGENT_LIST.add("Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)");
        USER_AGENT_LIST.add("Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/5.0)");
        USER_AGENT_LIST.add("Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/4.0; InfoPath.2; SV1; .NET CLR 2.0.50727; WOW64)");
        USER_AGENT_LIST.add("Mozilla/5.0 (compatible; MSIE 10.0; Macintosh; Intel Mac OS X 10_7_3; Trident/6.0)");
        USER_AGENT_LIST.add("Mozilla/4.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/5.0)");
        USER_AGENT_LIST.add("Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/532.2 (KHTML, like Gecko) ChromePlus/4.0.222.3 Chrome/4.0.222.3 Safari/532.2");
        USER_AGENT_LIST.add("Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.28.3 (KHTML, like Gecko) Version/3.2.3 ChromePlus/4.0.222.3 Chrome/4.0.222.3 Safari/525.28.3");
        USER_AGENT_LIST.add("Opera/9.80 (X11; Linux i686; Ubuntu/14.10) Presto/2.12.388 Version/12.16");
        USER_AGENT_LIST.add("Opera/9.80 (Windows NT 6.0) Presto/2.12.388 Version/12.14");
        USER_AGENT_LIST.add("Mozilla/5.0 (Windows NT 6.0; rv:2.0) Gecko/20100101 Firefox/4.0 Opera 12.14");
        USER_AGENT_LIST.add("Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.0) Opera 12.14");
        USER_AGENT_LIST.add("Opera/12.80 (Windows NT 5.1; U; en) Presto/2.10.289 Version/12.02");
        USER_AGENT_LIST.add("Opera/9.80 (Windows NT 6.1; U; es-ES) Presto/2.9.181 Version/12.00");
        USER_AGENT_LIST.add("Opera/9.80 (Windows NT 5.1; U; zh-sg) Presto/2.9.181 Version/12.00");
        USER_AGENT_LIST.add("Opera/12.0(Windows NT 5.2;U;en)Presto/22.9.168 Version/12.00");
        USER_AGENT_LIST.add("Opera/12.0(Windows NT 5.1;U;en)Presto/22.9.168 Version/12.00");
        USER_AGENT_LIST.add("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1");
        USER_AGENT_LIST.add("Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0");
        USER_AGENT_LIST.add("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10; rv:33.0) Gecko/20100101 Firefox/33.0");
        USER_AGENT_LIST.add("Mozilla/5.0 (X11; Linux i586; rv:31.0) Gecko/20100101 Firefox/31.0");
        USER_AGENT_LIST.add("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20130401 Firefox/31.0");
        USER_AGENT_LIST.add("Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0");
    }


    public static Map<String, String> defaultHeaders() {
        return defaultHeaders(null);
    }

    /**
     * 获取默认 header
     *
     * @param aimUrl 接口请求才传入，否则用 null {@link #defaultHeaders()}
     * @return headers
     */
    public static Map<String, String> defaultHeaders(String aimUrl) {
        Map<String, String> headers = new HashMap<>(16);
        headers.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
        headers.put("Accept-Language", "zh-CN,zh;q=0.9");
        headers.put("Connection", "keep-alive");
        headers.put("User-Agent", HeaderUtil.randomUserAgent());

        headers.put("Sec-Fetch-Dest", "document");
        headers.put("Sec-Fetch-Mode", "navigate");
        headers.put("Sec-Fetch-Site", "none");
        headers.put("Sec-Fetch-User", "?1");

        if (aimUrl != null) {
            // ajax/json 接口请求附带以下
            String host = HtmlUtil.parseDomainFromUrl(aimUrl);
            // 域名(和端口号)，一般在xhr请求中，DOC类型没有
            headers.put("Host", host);
            // 协议 + 域名，一般在xhr请求中，DOC类型没有

            // Origin 指示了请求来自于哪个站点，只有服务器名，不包含路径信息，浏览器自动添加到http请求 Header 中，无需手动设置。
            String origin = HtmlUtil.parseProtocolAndDomainFromUrl(aimUrl);
            headers.put("origin", origin);

            // url 去掉锚点信息，一般只存在于CORS跨域请求中
            String referer = aimUrl.split("#", 1)[0];
            headers.put("Referer", referer);
        }

//        headers.put("Upgrade-Insecure-Requests", "1");
//        headers.put("sec-ch-ua", "Not A;Brand\";v=\"99\", \"Chromium\";v=\"101\", \"Google Chrome\";v=\"101\"");
//        headers.put("sec-ch-ua-mobile", "?0");
//        headers.put("sec-ch-ua-platform", "\"Windows\"");
//        headers.put("Cache-Control", "no-cache");
//        headers.put("Pragma", "no-cache");
        //headers.put("Cookie", "channelid=0; sid=1647667211028616; _ga=GA1.2.1084681960.1647668232; _gid=GA1.2.329554079.1647668232; Hm_lvt_7ed65b1cc4b810e9fd37959c9bb51b31=1647668232; _gcl_au=1.1.798009929.1647668232; Hm_lpvt_7ed65b1cc4b810e9fd37959c9bb51b31=" + s);
        return headers;
    }

    public static String randomUserAgent() {
        int index = ThreadLocalRandom.current().nextInt(USER_AGENT_LIST.size());
        return USER_AGENT_LIST.get(index);
    }


}